package com.chejianer.push.config;


import com.corundumstudio.socketio.SocketIOServer;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.concurrent.Executor;

@Configuration
@ComponentScan(basePackages = {"com.chejianer.core.domain", "com.chejianer.admin.service"})
//@EnableTransactionManagement(proxyTargetClass = true)
@PropertySource(value = "classpath:/properties/app-${env}.properties")
@ImportResource("classpath:spring-config.xml")
@EnableAsync
public class DataConfig implements EnvironmentAware, AsyncConfigurer {

    private Environment environment;

    // customized executor pool for async call
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("AsyncExecutor-");
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }

//    @Bean
//    public DataSource dataSource(){
//        HikariDataSource ds = new HikariDataSource();
//        ds.setJdbcUrl(environment.getProperty("jdbc.url"));
//        ds.setUsername(environment.getProperty("username"));
//        ds.setPassword(environment.getProperty("password"));
//        ds.setDriverClassName(environment.getProperty("datasource.classname"));
//        ds.setConnectionTimeout(Long.parseLong(environment.getProperty("connection.timeout")));
//        ds.setMaximumPoolSize(Integer.parseInt(environment.getProperty("max.pool.size")));
//
//        return ds;
//    }

//    @Bean
//    public SqlSessionFactory sqlSessionFactory() throws Exception{
//        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
//        ssfb.setDataSource(dataSource());
////        Resource[] mapperLocations = new Resource[]{new ClassPathResource("/com/chejianer/core/domain/mapper/**/*.xml")};
////        ssfb.setMapperLocations(mapperLocations);
////        ssfb.setTypeHandlers(typeHandlers());
//
//        ssfb.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
//        return ssfb.getObject();
//    }


//    @Bean
//    public PlatformTransactionManager transactionManager(){
//        return new DataSourceTransactionManager(dataSource());
//    }

//    @Bean
//    public MapperScannerConfigurer mapperScannerConfigurer(){
//        MapperScannerConfigurer mapper = new MapperScannerConfigurer();
//        mapper.setBasePackage("com.chejianer.core.domain.mapper");
//        mapper.setAnnotationClass(Repository.class);
//        return mapper;
//    }

//    @Bean
//    public Jedis jedis(){
//        Jedis jedis = new Jedis(environment.getProperty("redis.host"), Integer.parseInt(environment.getProperty("redis.port")));
//        return jedis;
//    }

    @Bean
    public JedisPool jedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(Integer.parseInt(environment.getProperty("redis.max.idle")));
        config.setMaxTotal(Integer.parseInt(environment.getProperty("redis.max.total")));
        config.setMaxWaitMillis(Integer.parseInt(environment.getProperty("redis.max.waitmillis")));
        config.setMinIdle(Integer.parseInt(environment.getProperty("redis.min.idle")));
        String host = environment.getProperty("redis.host");
        Integer port = Integer.parseInt(environment.getProperty("redis.port"));
        String password = environment.getProperty("redis.password");
        JedisPool jedisPool = new JedisPool(config, host, port, 5000, password);
        return jedisPool;
    }

    @Bean
    public SocketIOServer socketIOServer(){
        com.corundumstudio.socketio.Configuration config=new com.corundumstudio.socketio.Configuration();
        config.setHostname(environment.getProperty("socketio.host"));
        config.setPort(Integer.parseInt(environment.getProperty("socketio.port")));
        final SocketIOServer server=new SocketIOServer(config);
        return server;
    }

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

}
